package com.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Product;
import com.service.ProductService;
import com.util.Conversion;
import com.util.JdbcUtil;
import com.util.PageUtil;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/product")
public class ProductServlet extends BaseServlet {
	ProductService productService=new ProductService();
		public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		 response.setContentType("text/html");
		 response.setCharacterEncoding("utf-8");
		//List<Product> products = jdbc.queryPreparedStatement(sql, Product.class);
		 List<Product> products = productService.selectproduct();
		/*Product p = products.get(1);
		System.out.println(p);*/
		//request.setAttribute("pageTool",pageTool);
		request.setAttribute("products",products);
		request.getRequestDispatcher("Typemanage.jsp").forward(request, response);
	}
	//出售商品查询（用户）,条件从传递对象中取出
	private void query_user(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer cp = Integer.parseInt(request.getParameter("cp")==null?"1":request.getParameter("cp"));
		Integer pagesize=4;
		Product product=new Product();
		Conversion.convert(product, request);
		List<Product> products = null;
		products = productService.query_user(product, cp, pagesize);
		int count =productService.count_user(product);
				
		String pageTool = PageUtil.getPageTool(request, count, cp, pagesize);
		request.setAttribute("pageTool",pageTool);
		
		request.setAttribute("products",products);
		request.getRequestDispatcher("products.jsp").forward(request, response);
	}
}
